package org.lhg.activiti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.lhg.activiti.model.News;
import org.lhg.activiti.model.PageBean;
import org.lhg.activiti.service.NewsService;
import org.lhg.activiti.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 新闻分页
     * @param page
     * @param limit
     * @param userId
     * @return
     */
    @RequestMapping(value = "/news_list",method = RequestMethod.GET)
    public CommonResult<JSONObject> news_list(@RequestParam(value="page",defaultValue="0")String page, @RequestParam(value="limit",defaultValue="0")String limit, String userId){
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(limit));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("userId",userId); // 用户名
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        List<News> leaveList=newsService.news_list(map);
        Long total=newsService.new_list_total(map);

        JSONObject result=new JSONObject();
        JSONArray jsonArray=JSONArray.parseArray(JSON.toJSONString(leaveList));
        result.put("rows", jsonArray);
        result.put("total", total);
        return CommonResult.success(result);
    }

    /**
     * 新增新闻
     * @param news
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/news_add",method = RequestMethod.POST)
    public CommonResult<JSONObject> news_add(@RequestBody News news) throws Exception {
        int resultTotal=0;
        news.setCreateTime(new Date());
        resultTotal=newsService.news_add(news);
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
        }else{
            result.put("success", false);
        }
        return CommonResult.success(result);
    }

    /**
     *  通过任务id获取新闻表单
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/news_by_processInstanceId",method = RequestMethod.POST)
    public CommonResult<JSONObject> news_by_processInstanceId(@RequestBody Map<String,Object> map)throws Exception{
        String processInstanceId=map.get("processInstanceId").toString();

        News news=newsService.news_by_processInstanceId(processInstanceId);
        JSONObject result=new JSONObject();
        result.put("news", JSONObject.parse(JSON.toJSONString(news)));
        return CommonResult.success(result);
    }

    /**
     * 完成审批
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/finshApply",method = RequestMethod.POST)
    public CommonResult<JSONObject> news_finsh_apply(@RequestBody Map<String,Object> map)throws Exception{
        String processInstanceId=map.get("processInstanceId").toString();
        News news=newsService.news_by_processInstanceId(processInstanceId);

        String state=map.get("state").toString();
        if(state.equals("2")){
            news.setNews_state("审核通过");
        }else{
            news.setNews_state("审核未通过");
        }
        newsService.news_update(news); // 修改新闻状态
        JSONObject result=new JSONObject();
        result.put("success", true);
        return CommonResult.success(result);
    }

}
